package com.boxtelecom.users;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.TimeZone;

import com.boxtelecom.chat.MyMessage;
import com.boxtelecom.signe.ListSigneAstro;
import com.boxtelecom.signe.SigneAstro;

import android.graphics.Bitmap;
import android.util.Log;

public class FacebookUser {
	private String _id;
	private String _name;
	private String _first_name;
	private String _last_name;
	private String _birthday;
	private String _signe_astro;
	private int _id_signe_astro;
	private String _fjid;
	private Presence _presence;
	private Bitmap _picture;
	private String _picture_path;
	private String _updated_time;
	private boolean _newMessage;
	private boolean _firstNewMessage;
	private ArrayList<MyMessage> _listMessage;
	private int _color;
	private int _color_presence;
	
	public FacebookUser(){
		_id = null;
		_name = null;
		_first_name = null;
		_last_name = null;
		_birthday = null;
		_signe_astro = null;
		_id_signe_astro = 0;
		_fjid = null;
		_presence = new Presence();
		_picture = null;
		_picture_path = null;
		_newMessage = false;
		_listMessage = null;
		_color = 0;
		_color_presence = 0;
		_firstNewMessage = true;		
	}

	public FacebookUser(String _id, String _name, String _birthday,
			String _signe_astro, String _fjid) {
		super();
		this._id = _id;
		this._name = _name;
		this._birthday = _birthday;
		this._signe_astro = _signe_astro;
		this._fjid = _fjid;
		_presence = new Presence();
		_picture = null;
		_picture_path = null;
		_newMessage = false;
		_listMessage = null;
		_color = 0;
		_firstNewMessage = true;
		_first_name = null;
		_last_name = null;
		_id_signe_astro = 0;
	}

	public void setId(String anId){
		_id = anId;
	}
	
	public void setName(String aName){
		_name = aName;
	}
	
	public void setFirstName(String aName){
		_first_name = aName;
	}
	
	public void setLastName(String aName){
		_last_name = aName;
	}
	
	public void setBirthday(String aBirthday){
		_birthday = aBirthday;
	}
	
	public void setSigneAstro(String aSigneAstro){
		_signe_astro = aSigneAstro;
	}
	
	public void setIdSigneAstro(int anId){
		_id_signe_astro = anId;
	}
	
	public String getId(){
		return _id;
	}
	
	public String getName(){
		return _name;
	}
	
	public String getFirstName(){
		return _first_name;
	}
	
	public String getLastName(){
		return _last_name;
	}
	
	public String getBirth(){
		return _birthday;
	}
	
	public String getSigneAstro(){
		return _signe_astro;
	}
	
	public int getIdSigneAstro(){
		return _id_signe_astro;
	}
	
	public String getFjid() {
		return _fjid;
	}

	public void setFjid(String aFjid) {
		this._fjid = aFjid;
	}
	
	public Presence getPresence(){
		return _presence;
	}
	
	public void setPresence(String type, String mode){
		this._presence.setType(type);
		this._presence.setMode(mode);
	}
	
	public void setPresence(String type){
		this._presence.setType(type);
	}

	public Bitmap getPicture() {
		return _picture;
	}

	public void setPicture(Bitmap aPicture) {
		this._picture = aPicture;
	}
	
	public void setPicturePath(String path){
		this._picture_path = path;
	}
	
	public String getPicturePath(){
		return _picture_path;
	}
	
	public void setUpdatedTime(String time){
		this._updated_time = time;
	}
	
	public String getUpdatedTime(){
		return _updated_time;
	}
	
	public boolean isNewMessage() {
		return _newMessage;
	}

	public void setNewMessage(boolean aNewMessage) {
		this._newMessage = aNewMessage;
	}

	public boolean isFirstNewMessage() {
		return _firstNewMessage;
	}

	public void setFirstNewMessage(boolean aFirstNewMessage) {
		this._firstNewMessage = aFirstNewMessage;
	}

	public ArrayList<MyMessage> getListMessage() {
		return _listMessage;
	}

	public void setListMessage(ArrayList<MyMessage> aListMessage) {
		this._listMessage = aListMessage;
	}

	public int getColor() {
		return _color;
	}

	public void setColor(int aColor) {
		this._color = aColor;
	}
	
	public int getColorPresence() {
		return _color_presence;
	}

	public void setColorPresence(int aColor) {
		this._color_presence = aColor;
	}

	public void deleteUser(){
		_id = null;
		_name = null;
		_first_name = null;
		_last_name = null;
		_birthday = null;
		_signe_astro = "null";
		_id_signe_astro = 0;
		_fjid = null;
		_newMessage = false;
		_listMessage = null;
		_color = 0;
		_firstNewMessage = true;
	}

	public void setSigneAstroFromBirthday(String aBirthday) {
		// TODO Auto-generated method stub
		
		TimeZone tz = TimeZone.getDefault();
		
		if(tz.getID().contains("America")){
			SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
			Date birthday = null;
			int daybirthday = 0;
			int monthbirthday = 0;
			
			if(aBirthday.length()<10){
				aBirthday = aBirthday + "/1989";
			}

			try {
				birthday = df.parse(aBirthday);
				daybirthday = birthday.getDate();
				monthbirthday = birthday.getMonth()+1;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
					
			ListSigneAstro list = new ListSigneAstro();
			Iterator<SigneAstro> it = list.getIterator();
			
			while(it.hasNext()){
				SigneAstro tmp = it.next();
				if(monthbirthday == tmp.get_begin_month()){
					if(daybirthday >= tmp.get_begin_day()){
						_signe_astro = tmp.getSigne();
					}
				}
				if(monthbirthday == tmp.get_end_month()){
					if(daybirthday <= tmp.get_end_day()){
						_signe_astro = tmp.getSigne();
					}
				}
			}
		} else {
			SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
			Date birthday = null;
			int daybirthday = 0;
			int monthbirthday = 0;
			if(aBirthday.length()<10){
				aBirthday = aBirthday + "/1989";
			}

			try {
				birthday = df.parse(aBirthday);
				daybirthday = birthday.getDate();
				monthbirthday = birthday.getMonth()+1;
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
					
			ListSigneAstro list = new ListSigneAstro();
			Iterator<SigneAstro> it = list.getIterator();
			
			while(it.hasNext()){
				SigneAstro tmp = it.next();
				if(monthbirthday == tmp.get_begin_month()){
					if(daybirthday >= tmp.get_begin_day()){
						_signe_astro = tmp.getSigne();
					}
				}
				if(monthbirthday == tmp.get_end_month()){
					if(daybirthday <= tmp.get_end_day()){
						_signe_astro = tmp.getSigne();
					}
				}
			}
		}
		
		
	}

	public boolean isNameNull() {
		// TODO Auto-generated method stub
		if(_name.equals("null")){
			return true;
		}
		return false;
	}
	
	public boolean hasSigneAstro(){
		if(_signe_astro.equals("null")){
			return false;
		}
		return true;
	}
	
	public void print(){
		Log.d("user", "id: " + getId());
		Log.d("user", "name: " + getName());
		Log.d("user", "first name: " + getFirstName());
		Log.d("user", "last name: " +  getLastName());
		Log.d("user", "birthday: " + getBirth());
		Log.d("user", "birthday: " + getSigneAstro());
		Log.d("user", "jid: " + getFjid());
		Log.d("user", "time: " + getUpdatedTime());
	}
	
	public void addMessage(MyMessage aMessage) {
		// TODO Auto-generated method stub
		if(_listMessage == null){
			_listMessage = new ArrayList<MyMessage>();
		}
		_listMessage.add(aMessage);
	}

	public ArrayList<String> getListMessageToArrayString() {
		// TODO Auto-generated method stub
		ArrayList<String> tmp = new ArrayList<String>();
		
		Iterator<MyMessage> it = _listMessage.iterator();
		
		while(it.hasNext()){
			MyMessage msg = it.next();
			tmp.add(msg.getUserName());
			tmp.add(msg.getUserMessage());
		}
		
		return tmp;
	}

	public boolean isPicturePathNull() {
		// TODO Auto-generated method stub
		if(_picture_path.equals("null")){
			return true;
		}
		return false;
	}
}
